# -*- encoding: utf-8 -*-
'''
@File    :   oss_util.py
@Time    :   2021/06/08 18:08:08
@Author  :   ZhuDong ，tangzhimiao

@Department   :  Magic Data Tech
@Desc    :   None
'''

import os, sys, glob, json
import shutil
import oss2

from tqdm import tqdm
import time
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


def delete(f):
    if os.path.isfile(f):
        os.remove(f)
    elif os.path.isdir(f):
        shutil.rmtree(f)

def mkdir(path):
    if os.path.isdir(path):
        return
    basedr, _ = os.path.split(path)
    if basedr:
        mkdir(basedr)
    os.mkdir(path)


def oss_download(wav_urls, wavdir='./'):
    def oss_download(wav_urls, wavdir='./'):
    auth = oss2.Auth('username', 'password')
    endpoint = 'http://oss-cn-beijing.aliyuncs.com'
    bucket = oss2.Bucket(auth, endpoint, 'cn-wav-crowd')

    logger.info("downloading audios ......")
    for wav, url in tqdm(wav_urls.items()):
        print(wav)
        path = os.path.join(wavdir, wav)  # 本地存储文件地址
        oss_file = url.split('com/')[-1]  # oss端文件对象

        if ' ' in wav:
            print('** %s' % path)
            continue

        exist = bucket.object_exists(oss_file)  # 返回值为true表示文件存在，false表示文件不存在。
        if not exist:  # 判断指定的文件在oss端是否存在
            logger.debug('%s not exist' % url)
            continue

        if not os.path.isfile(path):
            try:
                # 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
                bucket.get_object_to_file(oss_file, path)
            except Exception as e:
                logger.info(e, wav)

            # 删除小于1k的音频(导致提特征异常)
            if os.path.getsize(path) < 1000:
                delete(path)
        

def main():
    def main():
    wav_urls = {}
   # f = r'HY0001D200721S001B37_valid_2021_04_28_16....crowd'
    #f = r'HY0001D200721S001B38_valid_2021_05_07_20....crowd'
    f = r'HY0001D200721S001B53_valid_2021_05_11_17_47_33.crowd'
    wav_info = {'req_file': f}  # 记录一些包信息
    with open(f) as fp:
        for line in fp:
            j = json.loads(line)
            p_id, wave_name, wave_suf ,data = [j[x] for x in
                                          ['p_id', 'wave_name', 'wave_suf', 'data']]
            wave_name = wave_name + '.' + wave_suf
            #wav_urls[wave_name] = 'http://crowdwav.imagicdatatech.com/HY0001D200721S001B37/' + wave_name + '.' + wave_suf
            #wav_urls[wave_name] = 'http://crowdwav.imagicdatatech.com/HY0001D200721S001B38/' + wave_name + '.' + wave_suf
            wav_urls[wave_name] = 'http://crowdwav.imagicdatatech.com/HY0001D200721S001B53/' + wave_name
            print(wave_name,wave_suf)


    print(p_id, type(p_id))
    print(wave_name, wave_suf,data)

    # oss
    print('oss downloading ...')
    t0 = time.time()
    mkdir('./down4')
    print(wav_urls)
    oss_download(wav_urls, wavdir='./down4')
    print("oss download cost time: {:.2f}s".format(time.time()-t0))


if __name__ == "__main__":
    main()
